package org.galaxyworld.flycode.jobs;

import org.apache.commons.lang3.StringUtils;
import org.galaxyworld.flycode.utils.NameUtils;

import java.sql.Types;
import java.util.Objects;

public class Field {

    private String type;

    private String name;

    private String columnName;

    private boolean primaryKey;

    private boolean keyword;

    public String getSetterName() {
        return "set" + StringUtils.capitalize(name);
    }

    public String getGetterName() {
        return "get" + StringUtils.capitalize(name);
    }

    public String getType() {
        return type;
    }

    public String getName() {
        return name;
    }

    public boolean isPrimaryKey() {
        return primaryKey;
    }

    public boolean isKeyword() {
        return keyword;
    }

    public void setPrimaryKey(boolean isPrimaryKey) {
        this.primaryKey = isPrimaryKey;
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
        name = NameUtils.columnNameToMemberName(columnName);
        keyword = NameUtils.isKeyword(columnName);
    }

    public void setTypeId(int typeId, boolean java8) {
        switch (typeId) {
            case Types.BIT:
            case Types.BOOLEAN:
                this.type = "Boolean";
                break;
            case Types.TINYINT:
            case Types.SMALLINT:
            case Types.INTEGER:
                this.type = "Integer";
                break;
            case Types.BIGINT:
                this.type = "Long";
                break;
            case Types.FLOAT:
            case Types.REAL:
            case Types.DOUBLE:
            case Types.NUMERIC:
            case Types.DECIMAL:
                this.type = "Double";
                break;
            case Types.CHAR:
            case Types.VARCHAR:
            case Types.LONGVARCHAR:
            case Types.NCHAR:
            case Types.NVARCHAR:
            case Types.LONGNVARCHAR:
                this.type = "String";
                break;
            case Types.DATE:
            case Types.TIME:
            case Types.TIMESTAMP:
                this.type = java8 ? "LocalDateTime" : "Date";
                break;
            default:
                this.type = "String";
                break;
        }
    }
}
